import {
  _decorator,
  Input,
  Vec3,
  EventTouch,
  Prefab,
  instantiate,
  Node,
} from "cc";
import { BaseRoles } from "../BaseRoles";
import { BulletStateMachine } from "../Bullet/BulletStateMachine";
import { BulletEnum } from "../Enum/BulletEnum";
const { ccclass, property } = _decorator;
@ccclass("Player")
export class Player extends BaseRoles {
  private targetPos: Vec3 = new Vec3();

  @property(Prefab)
  bulletPrefab: Prefab = null;
  //子弹父节点
  @property(Node)
  bulletParent: Node = null;

  private shootTimer: number = 0;
  private shootInterval: number = 1; // 射击间隔时间
  onLoad() {
    //super.onLoad();
    // this.node.addComponent(BulletStateMachine);
  }
  start() {
    //super.start();
    this.node.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
  }

  update(deltaTime: number): void {
    super.update(deltaTime);
    // 更新射击计时器
    this.shootTimer += deltaTime;
    if (this.shootTimer >= this.shootInterval) {
      this.shootTimer = 0;
      // const bullet = instantiate(this.bulletPrefab);
      // bullet.setPosition(this.node.position);
      // this.bulletParent.addChild(bullet);
    }
  }

  protected updateMovement(deltaTime: number) {
    const currentPos = this.node.position;
    Vec3.lerp(currentPos, currentPos, this.targetPos, 10 * deltaTime);
    this.node.setPosition(currentPos);
  }

  private onTouchMove(event: EventTouch) {
    const location = event.getUILocation();
    const pos = new Vec3(location.x - 230, location.y - 380); // 根据屏幕中心点修正坐标

    // 限制移动范围（根据屏幕实际尺寸调整）
    this.targetPos.set(
      Math.min(Math.max(pos.x, -230), 230),
      Math.min(Math.max(pos.y, -380), 380),
      0
    );
  }
}
